window.onload = function() {
    // 导航栏   显示  隐藏 
    let li1 = document.querySelector('.nav-top ul .li2');
    let lis = document.querySelectorAll('.nav-top ul li');
    let ul1 = document.querySelector('.nav-top ul .li2 .ul2');
    for (let i = 0; i < lis.length; i++) {
        lis[i].addEventListener('mouseover', function() {
            if (lis[i].childNodes[3] != undefined) {
                // console.log(lis[i].childNodes[3]);
                lis[i].childNodes[3].style.display = 'block'
            }
        })
        lis[i].addEventListener('mouseout', function() {
            if (lis[i].childNodes[3] != undefined) {
                // console.log(lis[i].childNodes[3]);
                lis[i].childNodes[3].style.display = 'none'
            }
        })
    }

    // 轮播图
    let swiper1 = new Swiper(".mySwiper1", {
        spaceBetween: 30,
        centeredSlides: true,
        loop: true,
        autoplay: {
            delay: 2500,
            disableOnInteraction: false,
        },
        pagination: {
            el: ".swiper-pagination",
            clickable: true,
        },

    });
    let nav_top = document.querySelector('.nav');

    let li_top = document.createElement('li');
    li_top.style.width = '50px';
    li_top.style.height = '50px';
    li_top.style.position = 'fixed';
    li_top.style.top = '556px';
    li_top.style.right = '0px';
    li_top.style.border = '1px solid #9bd5ec8a';
    li_top.style.backgroundColor = '#dbdbdb6c';
    li_top.style.display = 'none';
    console.log(li_top);
    let bod = document.querySelector('body')
    bod.append(li_top);
    window.onscroll = function() {
        let t = document.documentElement.scrollTop;
        if (t >= 115) {
            nav_top.style.position = 'fixed';
            nav_top.left = '50' + '%';
        } else {
            nav_top.style.position = 'relative'
        }
        let s = document.documentElement.scrollTop;
        if (s >= 800) {
            li_top.style.display = 'block';
        } else {
            li_top.style.display = 'none';
        }
    }
    li_top.addEventListener('click', function() {
        // 里面的x和y不用跟单位 直接写数字即可
        // window.scroll(0, 0);
        // 因为是窗口滚动的 所以对象是window
        // animate(window, 0);
        animate(window, 0);
    });
    // 返回顶部
    function animate(obj, target, callback) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function() {
            console.log(window.pageYOffset);
            var step = (target - window.pageYOffset) / 10;
            // 因为步长有正值也有负值，正值往上取整数ceil  负值往下取整floor 然后在赋值给step
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (window.pageYOffset == target) {
                // 停止动画 本质是停止定时器
                clearInterval(obj.timer);
                if (callback) {
                    callback();
                }
            };
            window.scroll(0, window.pageYOffset + step);
        }, 20)
    }

}